import { ACTIONSTYPES } from '../constant';

const setRegistedApp = (data) => {
  return {
    type: ACTIONSTYPES.SET_REGISTED_APP,
    payload: data
  }
};

const setCurrentApp = (data) => {
  return {
    type: ACTIONSTYPES.SET_CURRENT_APP,
    payload: data
  }
};

const INIT_STATE = {
  apps: [],
  menus: [],
  currentApp: {},
  registeredApps: []
};

const reducer = (state = INIT_STATE, action) => {
  const { payload, type } = action;

  switch(type) {
    case ACTIONSTYPES.SET_REGISTED_APP: {
      return { ...state, registeredApps: state.registeredApps.concat(payload) };
    }

    case ACTIONSTYPES.SET_CURRENT_APP: {
      const currentApp = {
        ...state.currentApp,
        ...payload
      };
      return { ...state, currentApp };
    }

    case ACTIONSTYPES.GET_MENU_DATA: {
      return {
        ...state,
        ...payload
      }
    }
    
    default:
      return state;
  }
};

const reducers = {
  global: reducer
};

export const actions = {
  setRegistedApp,
  setCurrentApp
}

export default reducers;
